package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.DApplyforLoansMapper;
import com.weipai.demo.mapper.PayOrderMapper;
import com.weipai.demo.mapper.base.ApplyforLoansMapper;
import com.weipai.demo.mapper.base.DUserMapper;
import com.weipai.demo.mapper.base.LoansClassMapper;
import com.weipai.demo.model.ApplyforLoans;
import com.weipai.demo.model.DUser;
import com.weipai.demo.model.LoansClass;
import com.weipai.demo.model.PayOrder;
import com.weipai.demo.service.ApplyforLoansService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import com.weipai.demo.util.VerificationCodeImgUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/28 17:39
 * @Version 1.0
 */
@Service
public class ApplyforLoansServiceImpl implements ApplyforLoansService {

    private static final Logger log = LoggerFactory.getLogger(CarPriceServiceImpl.class);

    @Autowired
    private ApplyforLoansMapper applyforLoansMapper;
    @Autowired
    private DApplyforLoansMapper dApplyforLoansMapper;
    @Autowired
    private LoansClassMapper loansClassMapper;
    @Autowired
    private PayOrderMapper payOrderMapper;
    @Autowired
    private DUserMapper dUserMapper;

    //页面用户申请贷款
    @Override
    public Response applyFor(ApplyforLoans applyforLoans) {
        Response response = new Response();
        try {
            applyforLoans.setStatus(0);
            applyforLoans.setReveal(1);
            Date date = new Date();
            applyforLoans.setCreateTime(date);
            int insert = applyforLoansMapper.insert(applyforLoans);
            response.setCode(RetCode.APPLYFORSUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("applyFor" + e.getMessage());
        }
        return response;
    }

    //分页展示贷款信息
    @Override
    public LayuiTablePagingResult showApplyFor(Integer page, Integer size) {
        LayuiTablePagingResult result = null;
        try {
            PageHelper.startPage(page, size);
            Example example = new Example(ApplyforLoans.class);
            example.setOrderByClause("id desc");
            //根据状态查询申请的用户
            List<ApplyforLoans> banners = applyforLoansMapper.selectByExample(example);

            PageInfo<ApplyforLoans> pageInfo = new PageInfo<>(banners);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("showApplyFor" + e.getMessage());
        }
        return result;
    }


    //申请用户是否通过审核
    @Override
    public Response audit(Integer id, Integer status) {
        Response response = new Response();
        try {
            ApplyforLoans applyforLoans = new ApplyforLoans();
            applyforLoans.setStatus(status);
            applyforLoans.setId(id);
            if (id != null) {
                //int i = applyforLoansMapper.updateByPrimaryKey(applyforLoans);
                dApplyforLoansMapper.updateByid(id, status);
                response.setCode(RetCode.UPDATESUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("audit" + e.getMessage());
        }
        return response;
    }

    //申请贷款用户删除
    @Override
    public Response delete(Integer id) {
        Response response = new Response();
        if (id != null) {
            ApplyforLoans applyforLoans = new ApplyforLoans();
            applyforLoans.setId(id);
            int delete = applyforLoansMapper.delete(applyforLoans);
            response.setCode(RetCode.SUCCESS);
        }
        return response;
    }

    //图片随机点击
    @Override
    public void dynamic(HttpServletRequest request, HttpServletResponse response) {
//        Response response1=new Response();
        String type = request.getParameter("m");
        try {
            // 设置响应的类型格式为图片格式
            response.setContentType("image/jpeg");
            //禁止图像缓存。
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);

            HttpSession session = request.getSession();

            VerificationCodeImgUtil vCode = new VerificationCodeImgUtil(200, 50, 4, 50);
            session.setAttribute("code" + type, vCode.getCode());
            vCode.write(response.getOutputStream());
//            response1.put("vCode",vCode);
        } catch (IOException e) {
//            response1.setMsg(e.getMessage());
//            response1.setCode(RetCode.REQUEST_ERROR);
            e.printStackTrace();
        }
//        return response1;

    }

    //添加贷款分类
    @Override
    public Response save(String loansClassName, String introduce, String status) {
        Response response = null;
        try {
            response = new Response();

            LoansClass loansClass = new LoansClass();
            loansClass.setIntroduce(introduce);
            loansClass.setLoansClassName(loansClassName);
            loansClass.setStatus(status);
            Date date = new Date();
            loansClass.setCreateTime(date);

            int insert = loansClassMapper.insert(loansClass);
            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("save" + e.getMessage());
        }
        return response;
    }

    //检测是否支付了
    @Override
    public Response checkPay(String phone) {
        Response response = null;
        try {
            response = new Response();
            Example example = new Example(PayOrder.class);
            example.createCriteria().andEqualTo("mobile", phone).andEqualTo("status", 2);
            PayOrder payOrder = payOrderMapper.selectOneByExample(example);
            if (payOrder != null) {
                DUser dUser = new DUser();
                dUser.setUserName(phone);
                DUser user = dUserMapper.selectOne(dUser);
                if (user != null) {
                    DUser dUser1 = new DUser();
                    dUser1.setStatus(1);
                    dUser1.setId(user.getId());
                    dUserMapper.updateByPrimaryKeySelective(dUser1);
                }
                response.setCode(RetCode.SUCCESS);
            } else {
                response.setCode(RetCode.REQUEST_ERROR);
                response.setMsg("请先完成支付");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(RetCode.REQUEST_ERROR);
            response.setMsg(e.getMessage());
        }
        return response;
    }

}
